package com.teliasonera.crm.testing;

import java.util.Date;
import java.util.Map;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Select;

public class CreateAccountPage extends CRMPage {
    
    public static final String ACCOUNT_NAME    = "_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id13_customForm_WAR_CustomForm_INSTANCE_nFj2_";
    public static final String BILLING_TYPE    = "_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id277_customForm_WAR_CustomForm_INSTANCE_nFj2_";
    
    public final static String POSTAL_CODE     = "_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id280_customForm_WAR_CustomForm_INSTANCE_nFj2_";
    public final static String STREET_NAME     = "_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id285_customForm_WAR_CustomForm_INSTANCE_nFj2_";
    public final static String STREET_NUMBER   = "_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id288_customForm_WAR_CustomForm_INSTANCE_nFj2_";
    public final static String ADDRESS_VALIDATION_STATUS = "_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id348_customForm_WAR_CustomForm_INSTANCE_nFj2_";
    
    public static final String CHOOSE_ADDRESS  = "_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id259_customForm_WAR_CustomForm_INSTANCE_nFj2_";
    public static final String ADDRESS         = "_idJsp2_CustomTable_WAR_CustomTable_INSTANCE_IyB8_:dataScroller_CustomTable_WAR_CustomTable_INSTANCE_IyB8_:1:_id3_CustomTable_WAR_CustomTable_INSTANCE_IyB8_";
    public static final String INVOICE_METHOD  = "_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id476_customForm_WAR_CustomForm_INSTANCE_nFj2_";
    public static final String CREATE_BUTTON   = "_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id535_customForm_WAR_CustomForm_INSTANCE_nFj2_";

    public CreateAccountPage(WebDriver newDriver) {
        super(newDriver, PAGE_TITLE_CREATE_ACCOUNT);
    }
    
    public OrderListPage createAccount(Map accountDetails) {
        
        log("Wait for New Account Name box");
        driver.findElement(By.id(ACCOUNT_NAME)).sendKeys((String)accountDetails.get(ACCOUNT_NAME));
        
        log("Select billing address type");
        new Select(driver.findElement(By.id(BILLING_TYPE))).selectByVisibleText("Physical");
        waitForPageToLoad(60);
        
        try {
            if(accountDetails.get(STREET_NAME) == null) throw new Exception("No address given");
            populateAddress(accountDetails);
        } catch (Exception e) {
            log("Press Choose Existing Address");
            driver.findElement(By.id(CHOOSE_ADDRESS)).click();

            log("Wait for address list");
            waitForPageToLoad(60);
            driver.findElement(By.id(ADDRESS)).click();
        }
        
        waitForPageToLoad(60);
        
        new Select(driver.findElement(By.id(INVOICE_METHOD))).selectByVisibleText("Paper invoice");
        
        log("Wait for invoice method to complete");
        waitForPageToLoad(60);
        
        // Submit a new account
        log("Submit new account data");
        driver.findElement(By.id(CREATE_BUTTON)).click();
        
        return new OrderListPage(driver);
        
    }
    
    public void populateAddress(Map addressDetails) throws Exception {
        
        long startTime = new Date().getTime();
    
        /*
         * Select postal code field, type postal code and wait for page to load
         **/ 
        log("Entering postal code");
        driver.findElement(By.id(POSTAL_CODE)).sendKeys((String)addressDetails.get(POSTAL_CODE));
        driver.findElement(By.id(STREET_NAME)).click();
        waitForLoadingToFinish(60);
        
        /*
         * Select address field, type address, wait for suggestion to appear and select from suggestion
         **/ 
        log("Entering street name");
        driver.findElement(By.id(STREET_NAME)).sendKeys((String)addressDetails.get(STREET_NAME));
        waitForElementPresent(By.xpath("//div/div[text()[contains(.,'" + (String)addressDetails.get(STREET_NAME) + "')]]"), 30);
        driver.findElement(By.xpath("//div/div[text()[contains(.,'" + (String)addressDetails.get(STREET_NAME) + "')]]")).click();
        driver.findElement(By.id(STREET_NUMBER)).click();
        waitForLoadingToFinish(60);
        
        /*
         * Select address number field Wait for suggestion to appear Select from suggestion
         **/ 
        log("Entering street number");
        driver.findElement(By.id(STREET_NUMBER)).sendKeys((String)addressDetails.get(STREET_NUMBER));
        waitForElementPresent(By.xpath("//div/div[text()[contains(.,'" + (String)addressDetails.get(STREET_NUMBER) + "')]]"), 30);
        driver.findElement(By.xpath("//div/div[text()[contains(.,'" + (String)addressDetails.get(STREET_NUMBER) + "')]]")).click();
        waitForLoadingToFinish(60);
        
        long stopTime = new Date().getTime();
        long elapsedTime = stopTime - startTime;
        
        log("Address population took: " + elapsedTime + " milliseconds");
        
        // Check that address is validated
        Select addressValidation = new Select(driver.findElement(By.id(ADDRESS_VALIDATION_STATUS)));
        String selected = addressValidation.getFirstSelectedOption().getText();
        if(!selected.equals("Validated")) throw new Exception ("Address not validated");
        
    }
}